const path = require("path");

// 发布新文章的处理函数
exports.addArticle = (req, res) => {
  // 标题、内容、状态、所属的分类Id
  const articleInfo = {
    ...req.query,
    // 文章发布时间
    publishDate: new Date(),
    // 文章作者的Id
    authorId: req.user.id,
    // 文章作者的昵称
    nickname: req.user.nickname,
  };
  const sql = `insert into article_detail set ?`;
  // 导入数据库操作模块
  const db = require("../db/index");
  // 执行 SQL 语句
  db.query(sql, articleInfo, (err, results) => {
    console.log(results);
    // 执行 SQL 语句失败
    if (err) return res.cc(err);
    // 执行 SQL 语句成功，但是影响行数不等于 1
    if (results.affectedRows !== 1) return res.cc("发布文章失败！");
    // 发布文章成功
    res.cc("发布文章成功", 0);
  });

  // res.send('ok')
};

// 获取文章页数(允许前端指定一页有多少条感想)
exports.getArticlePage = (req, res) => {
  const sql = "select count(*) as 'num' from article_detail";

  const db = require("../db/index");
  db.query(sql, (err, results) => {
    // 执行sql语句失败
    if (err) return res.cc(err);
    // 成功
    res.send({
      status: 0,
      message: "获取页数成功成功",
      data: results,
    });
  });
};

// 获取文章信息（分页处理）
exports.list = (req, res) => {
  const { preVal = 1, count = 5 } = req.body;
  const truePreVal = (preVal - 1) * count;
  let hasNextPage = false;
  // 从数据库中获取到接口
  const sql =
    "select * from article_detail where idDelete=0 order by publishDate desc limit ?,?";

  const db = require("../db/index");
  db.query(sql, [truePreVal, count + 1], (err, results) => {
    if (results.length > count) {
      hasNextPage = true;
      results.pop();
    }
    // 执行sql语句失败
    if (err) return res.cc(err);
    // 成功
    res.send({
      status: 0,
      message: "获取文章信息成功",
      data: results,
      hasNextPage,
    });
  });
};

// 用户点赞
exports.like = (req, res) => {
  const { id, username, islike } = req.query;
  const db = require("../db/index");

  const sql =
    islike == "true"
      ? `update article_detail set like_num=like_num+1, like_ids=CONCAT(like_ids,'${username},') where id=${id}`
      : `update article_detail set like_num=like_num-1, like_ids=replace(like_ids,'${username},','') where id=${id}`;
  db.query(sql, (err, results) => {
    // 执行sql语句失败
    if (err) return res.cc(err);
    // 成功
    res.send({
      status: 0,
      message: islike ? "点赞成功" : "取消点赞成功",
      data: results,
    });
  });
};

// 用户收藏
exports.collect = (req, res) => {
  const { id, username, iscollect } = req.query;
  const db = require("../db/index");

  const sql =
    iscollect == "true"
      ? `update article_detail set collect_num=collect_num+1, collect_ids=CONCAT(collect_ids,'${username},') where id=${id}`
      : `update article_detail set collect_num=collect_num-1, collect_ids=replace(collect_ids,'${username},','') where id=${id}`;
  db.query(sql, (err, results) => {
    // 执行sql语句失败
    if (err) return res.cc(err);
    // 成功
    res.send({
      status: 0,
      message: iscollect ? "收藏成功" : "取消收藏成功",
      data: results,
    });
  });
};

// 获取收藏文章
exports.getCollect = (req, res) => {
  const { username } = req.query;
  const db = require("../db/index");

  const sql = `select * from article_detail where collect_ids like '%${username}%'`;
  db.query(sql, (err, results) => {
    // 执行sql语句失败
    if (err) return res.cc(err);
    // 成功
    res.send({
      status: 0,
      message: "获取收藏成功",
      data: results,
    });
  });
};
